package com.qiwenshare.file.sm2Server;

import cn.hutool.json.JSONObject;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Component
@Data
public class Sm2Utils {

  @Autowired private RestTemplate restTemplate;

  public String getSm2Key() {
    Map<String, String> map = new HashMap<>();
    map.put("alg", "SM2");
    map.put("len", "256");
    map.put("thirdUserId", "zhumin11281432");
    String url = "http://111.20.164.182:9870/cloudSign/getPubKey";
    ResponseEntity<String> res = restTemplate.postForEntity(url, map, String.class);
    System.out.println(res.getBody());
    JSONObject jsonObject = new JSONObject(res.getBody());
    JSONObject head = jsonObject.getJSONObject("head");
    JSONObject responseInfo = head.getJSONObject("responseInfo");
    System.out.println("publicKey: " + responseInfo.get("publickey"));
    return responseInfo.get("publickey").toString();
  }

  public String getSm2Key(HashMap<String, String> map) {
    //    map.put("alg", "SM2");
    //    map.put("len", "256");
    //    map.put("thirdUserId", "zhumin11281432");
    String url = "http://111.20.164.182:9870/cloudSign/getPubKey";
    ResponseEntity<String> res = restTemplate.postForEntity(url, map, String.class);
    System.out.println(res.getBody());
    JSONObject jsonObject = new JSONObject(res.getBody());
    JSONObject head = jsonObject.getJSONObject("head");
    JSONObject responseInfo = head.getJSONObject("responseInfo");
    System.out.println("publicKey: " + responseInfo.get("publickey"));
    return responseInfo.get("publickey").toString();
  }

  public Object getSm2KeyBody(HashMap<String, String> map) {
    String url = "http://111.20.164.182:9870/cloudSign/getPubKey";
    ResponseEntity<String> res = restTemplate.postForEntity(url, map, String.class);
    return res.getBody();
  }

  public String sm2Enc() {
    Map<String, String> map = new HashMap<>();
    //    map.put("Plaintext", "MzIxMjMyMzIzMQ==");
    String plaintext =
        Base64.getEncoder().encodeToString("hello world".getBytes(StandardCharsets.UTF_8));
    map.put("Plaintext", plaintext);
    map.put(
        "pubKey",
        "qcRiMXAcMygZy6TyM/MSXcTAOArGFeGZjUso+ohGKa6BvmvCGDyVrPbV+8218MpuSzsD27t/xv/qHeqKOoWMtw==");
    String url = "http://111.20.164.182:9870/cloudSign/encryptEcc";

    ResponseEntity<String> res = restTemplate.postForEntity(url, map, String.class);
    JSONObject jsonObject = new JSONObject(res.getBody());
    JSONObject head = jsonObject.getJSONObject("head");
    System.out.println("head: " + head.toString());
    JSONObject responseInfo = head.getJSONObject("responseInfo");
    System.out.println();
    System.out.println("plaintextEncData: " + responseInfo.get("plaintextEncData").toString());
    return responseInfo.get("plaintextEncData").toString();
  }

  public String sm2Enc(Map<String, String> map) {
    /*    map.put("Plaintext", "MzIxMjMyMzIzMQ==");
    map.put(
        "pubKey",
        "qcRiMXAcMygZy6TyM/MSXcTAOArGFeGZjUso+ohGKa6BvmvCGDyVrPbV+8218MpuSzsD27t/xv/qHeqKOoWMtw==");*/

    String url = "http://111.20.164.182:9870/cloudSign/encryptEcc";
    ResponseEntity<String> res = restTemplate.postForEntity(url, map, String.class);
    JSONObject jsonObject = new JSONObject(res.getBody());
    JSONObject head = jsonObject.getJSONObject("head");
    JSONObject responseInfo = head.getJSONObject("responseInfo");
    return responseInfo.get("plaintextEncData").toString();
  }

  public Object sm2EncBody(Map<String, String> map) {
    String url = "http://111.20.164.182:9870/cloudSign/encryptEcc";
    ResponseEntity<String> res = restTemplate.postForEntity(url, map, String.class);
    return res.getBody();
  }

  public String sm2Dec() {
    Map<String, String> map = new HashMap<>();
    map.put(
        "encData",
        "KW8D02/Y62Yvu2KgMeIpDuLUjU97t2252QlHMYtfRQxP4OvPh7/OmfLe2EDuzjNsuZ8k3IikIvrFHGjJ5an0KZq0bL9a9e3Gr513t+labjYAG2XVAHjz3nlaOK4yflYs0wV+KHCpZL5rTw==");
    map.put("thirdUserId", "1577255345955");
    map.put("pin", "123456");
    map.put("len", "256");
    map.put("alg", "SM2");
    String url = "http://111.20.164.182:9870/cloudSign/decryptEcc";
    ResponseEntity<String> res = restTemplate.postForEntity(url, map, String.class);
    JSONObject jsonObject = new JSONObject(res.getBody());
    JSONObject head = jsonObject.getJSONObject("head");
    return head.toString();
  }

  public String sm2Dec(Map<String, String> map) {
    map.put(
        "encData",
        "KW8D02/Y62Yvu2KgMeIpDuLUjU97t2252QlHMYtfRQxP4OvPh7/OmfLe2EDuzjNsuZ8k3IikIvrFHGjJ5an0KZq0bL9a9e3Gr513t+labjYAG2XVAHjz3nlaOK4yflYs0wV+KHCpZL5rTw==");
    map.put("thirdUserId", "1577255345955");
    map.put("pin", "123456");
    map.put("len", "256");
    map.put("alg", "SM2");
    String url = "http://111.20.164.182:9870/cloudSign/decryptEcc";
    ResponseEntity<String> res = restTemplate.postForEntity(url, map, String.class);
    JSONObject jsonObject = new JSONObject(res.getBody());
    JSONObject head = jsonObject.getJSONObject("head");
    return head.toString();
  }

  public Object sm2DecBody(Map<String, String> map) {
    String url = "http://111.20.164.182:9870/cloudSign/decryptEcc";
    ResponseEntity<String> res = restTemplate.postForEntity(url, map, String.class);
    return res.getBody();
  }
}
